import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import { loadUserId, loadVillageId } from '../common/js/cache'
import qs from 'qs'

// 获取姓名和电话号码
export function getUser () {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getUser`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取验证码
export function getIndentCode (phoneNum) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/sendCode`
  const data = {
    phone: phoneNum,
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋信息的数据
export function getHomeDate (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getHouses?areas_id=${id}`
  return axios({
    method: 'GET',
    url: url
  }).then(res => {
    return Promise.resolve(res.data)
  }).catch(err => {
    return Promise.reject(err)
  })
}

// 获取房号的信息
export function getRoomNum (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getUnit`
  const data = {
    danyuan_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交业主认证信息(微信浏览器)
export function subData (code, id, area, houseNum, did, serverId, phone, other) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/add`
  const data = {
    code: code,
    user_id: loadUserId(),
    areasId: id,
    area: area,
    house_num: houseNum,
    danyuan_id: did,
    serverIds: serverId,
    phone: phone,
    other: other
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交业主认证信息(普通浏览器)
export function subOrData (code, id, area, houseNum, did, img, phone, other) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/webAdd`
  const data = {
    code: code,
    user_id: loadUserId(),
    areasId: id,
    area: area,
    house_num: houseNum,
    danyuan_id: did,
    imgs: img,
    phone: phone,
    other: other
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证列表数据
export function getidentList () {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getDetail`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证详情页面
export function getidentDetail (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getOneDetail`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证详情页面删除
export function deleteOne (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/deleteOne`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的投票详情(老版)
export function getResult (id, type) {
  const url = `${commonUrl.apihost}/index.php/home/my_vote/getDetail`
  const data = {
    userId: loadUserId(),
    type: type,
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 新版
// 选举结果（候选人为选项）
export function getManDetai (id) {
  const url = `${commonUrl.apihost}/index.php/applets/voting_result/getCandidateResult`
  const data = {
    user_id: loadUserId(),
    voting_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 选举结果（候选人为议题）
export function getManDetai2 (id) {
  const url = `${commonUrl.apihost}/index.php/applets/voting_result/getNewCandidateResult`
  const data = {
    user_id: loadUserId(),
    voting_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 事物结果数据
export function getDetail (id) {
  const url = `${commonUrl.apihost}/index.php/applets/voting_result/getSupplementResult`
  const data = {
    user_id: loadUserId(),
    voting_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 满意度投票结果
export function getSatData (id) {
  const url = `${commonUrl.apihost}/index.php/applets/voting_result/getSatisfactionResult`
  const data = {
    user_id: loadUserId(),
    voting_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 新版选举结果查看候选人简介
export function getNewPerSonal (id) {
  const url = `${commonUrl.apihost}/index.php/applets/supplement/getVotingCandidateDetail`
  const data = {
    candidate_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 新版投票结果记录列表新版查看详细内容
export function getNewDetailData (id) {
  const url = `${commonUrl.apihost}/index.php/applets/supplement/getVotingDetails`
  const data = {
    id: id,
    user_id: loadUserId(),
    areas_id: loadVillageId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 新版投票记录（事务，满意度）结果页面的查看详情
export function getNewVoteDetail(id) {
  const url = `${commonUrl.apihost}/index.php/applets/supplement/getSupplementDetails`
  const data = {
    id: id,
    user_id: loadUserId(),
    areas_id: loadVillageId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
